import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col, Card} from 'antd';
import {agentKind} from '../Enum';
import {FORM_OPTIONS} from '../constants';
import TextInput from 'Shared/components/TextInput';
import WrappedSelect from '../common/WrappedSelect';
import {injectIntl, language} from './intl';
const FormItem = Form.Item;

const agentKindToList = agentKind.toList(language);
class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }

    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    render() {
        const {formatMessage} = this.props.intl;
        return (
            <div>
                <Form className="form-standard">
                    <Card>
                        <Row>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.code',
                                    defaultMessage: '编号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="code"
                                        value={this.props.conditions.code}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.name',
                                    defaultMessage: '名称'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="name"
                                        value={this.props.conditions.name}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.shortCode',
                                    defaultMessage: '简码'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="shortCode"
                                        value={this.props.conditions.shortCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.shortName',
                                    defaultMessage: '简称'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="shortName"
                                        value={this.props.conditions.shortName}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.kind',
                                    defaultMessage: '客户分类'
                                })}{...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="kind"
                                        options={agentKindToList}
                                        value={this.props.conditions.kind}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>{formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}</Button>
                                <Button key="clear" onClick={this.props.onResetClick}>{formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}</Button>
                            </Col>
                        </Row>
                    </Card>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
